/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain an copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.view;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.DialogPickerValueChange;
import com.xuexiang.xuidemo.util.PickerValueChange;
import com.xuexiang.xuidemo.widget.CityPicker;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * 描述 省市区三级中心弹框联动
 *
 * @author wjt
 * @since 2021-04-09
 */
public class CityPickerCenterDialog {
    private static final int THREE = 3;
    private static final int EIGHT = 8;
    private static final int TEN = 10;
    private Context context;
    private CommonDialog mCommonDialog = null;
    //private String selctMessage = "";
    private DialogPickerValueChange dialogPickerValueChange;
    private  AnimatorProperty animatorProperty;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public CityPickerCenterDialog(Context context) {
        this.context = context;
        create();
    }

    private void create() {
        if (mCommonDialog == null) {
            Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
            int width = display.getAttributes().width;
            mCommonDialog = new CommonDialog(context);
            Component component1 = LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_ability_picture2, null, true);
            initComponent(component1, mCommonDialog);
            int GEN = width/TEN;
            int aa1 =   AttrHelper.vp2px(273,context);
            mCommonDialog.setSize(GEN*EIGHT, aa1); //50
            mCommonDialog.setContentCustomComponent(component1);
            mCommonDialog.setAutoClosable(true);
            mCommonDialog.setTransparent(true);
            animatorProperty = createAnimCenter();
            animatorProperty.setTarget(component1);
            mCommonDialog.setAlignment(LayoutAlignment.CENTER);
        }
    }

    private void initComponent(Component component1, final CommonDialog commonDialog) {
        final Text cancelBtn = (Text) component1.findComponentById(ResourceTable.Id_btn_cancel);
        final Text okBtn = (Text) component1.findComponentById(ResourceTable.Id_btn_ok);
        final CityPicker cityPicker = (CityPicker) component1.findComponentById(ResourceTable.Id_opv_city);
        cityPicker.setValuChange(new PickerValueChange() {
            @Override
            public void valueChange(String province, String city, String county) {
                //selctMessage = province + city + county;
            }
        });
        cancelBtn.setClickedListener(component -> {
            if (mCommonDialog != null) {
                mCommonDialog.hide();
                mCommonDialog.remove();
            }
        });
        okBtn.setClickedListener(component -> {
            if (dialogPickerValueChange != null) {
                dialogPickerValueChange.valueChange(cityPicker.getProvinceValue()
                        + ","
                        + cityPicker.getCityValue()
                        + ","
                        + cityPicker.getCountyValue());
            }
            if (mCommonDialog != null) {
                mCommonDialog.hide();
                mCommonDialog.remove();
            }
        });
    }

    /**
     * 显示show
     */
    public void show() {
        if (mCommonDialog != null) {
            mCommonDialog.show();
            animatorProperty.start();
            mCommonDialog.setTransparent(true);
        }
    }

    public void setDialogPickerValueChange(DialogPickerValueChange dialogPickerValueChange) {
        this.dialogPickerValueChange = dialogPickerValueChange;
    }

    private AnimatorProperty createAnimCenter() {
        AnimatorProperty animator=new AnimatorProperty();
        animator.scaleX(1).scaleXFrom(0).scaleY(1).scaleYFrom(0).setDuration(200);
        return  animator;
    }
}
